<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="150px" @submit.native.prevent>
      <el-row>
        <el-col :span="6">
          <el-form-item label="所属单位" prop="taskName">
            <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions"
                         :props="categoryProps" style="width:100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="姓名" prop="type">
            <el-input v-model="pram.name" maxlength="100" placeholder="姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="人员类别" prop="personnelCategory">
            <el-input v-model="pram.personnelCategory" maxlength="100" placeholder="人员类别"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别" prop="taskType">
            <el-select v-model="pram.gender" placeholder="请选择">
              <el-option
                v-for="item in genderOption"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="年龄" prop="age">
            <el-input v-model="pram.age" maxlength="100" placeholder="年龄"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="体重(KG)" prop="weight">
            <el-input v-model="pram.weight" maxlength="100" placeholder="体重"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="身高(M)" prop="height">
            <el-input v-model="pram.height" maxlength="100" placeholder="身高"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="BMI" prop="bmi">
            <el-input v-model="pram.bmi" maxlength="100" placeholder="BMI"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="体型评定" prop="shape">
            <el-input v-model="pram.shape" maxlength="100" placeholder="体型评定"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仰卧起坐个数" prop="sitUpsNumber">
            <el-input v-model="pram.sitUpsNumber" maxlength="100" placeholder="仰卧起坐个数"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仰卧起坐分数" prop="sitUpsFraction">
            <el-input v-model="pram.sitUpsFraction" maxlength="100" placeholder="仰卧起坐分数"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="30X2蛇形跑时间(秒)" prop="snakeRunTime">
            <el-input v-model="pram.snakeRunTime" maxlength="100" placeholder="蛇形跑时间"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="30X2蛇形跑分数" prop="snakeRunFraction">
            <el-input v-model="pram.snakeRunFraction" maxlength="100" placeholder="蛇形跑分数"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="3000米时间(分)" prop="threeTime">
            <el-input v-model="pram.threeTime" maxlength="100" placeholder="3000米时间"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="3000米分数" prop="threeFraction">
            <el-input v-model="pram.threeFraction" maxlength="100" placeholder="3000米分数"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="引体向上个数" prop="testNumber">
            <el-input v-model="pram.testNumber" maxlength="100" placeholder="引体向上个数"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="引体向上时间" prop="testTime">
            <el-input v-model="pram.testTime" maxlength="100" placeholder="引体向上时间"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="引体向上分数" prop="testFraction">
            <el-input v-model="pram.testFraction" maxlength="100" placeholder="引体向上分数"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="总分" prop="totalScore">
            <el-input v-model="pram.totalScore" maxlength="100" placeholder="总分"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="成绩等级" prop="gradeLevel">
            <el-input v-model="pram.gradeLevel" maxlength="100" placeholder="成绩等级"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="序号">
        <el-input-number v-model="pram.number" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')"
        >{{ isCreate === 0 ? '确定' : '更新' }}
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as api from '@/api/sports'
import { Debounce } from '@/utils/validate'
import { insertMilitarySportsApi, selectMilitarySportsByIdApi } from '@/api/sports'

export default {
  components: {},
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    id: {
      type: Number,
      required: true
    },
    allCategoryData: {
      type: Object
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      constants: this.$constants,
      pram: {
        name: null,
        personnelCategory: null,
        gender: null,
        age: null,
        weight: null,
        height: null,
        bmi: null,
        shape: null,
        sitUpsNumber: null,
        sitUpsFraction: null,
        snakeRunTime: null,
        snakeRunFraction: null,
        threeTime: null,
        threeFraction: null,
        testNumber: null,
        testTime: null,
        testFraction: null,
        totalScore: null,
        gradeLevel: null,
        number: 0,
        departmentId: '',
        id: 0
      },
      genderOption: [
        {id:1,name: '男'},
        {id:2,name: '女'},

      ],
      rules: {
        // name: [{ required: true, message: '请填设备名称', trigger: ['blur', 'change'] }],
        // brand: [{ required: true, message: '请选择设备品牌', trigger: ['blur', 'change'] }],
        // device: [{ required: true, message: '请选择设备型号', trigger: ['blur', 'change'] }],
        // deviceType: [{ required: true, message: '请选择设备类型', trigger: ['blur', 'change'] }],
        // personId: [ { required: true, message: '请选择所属人', trigger: ['blur', 'change']} ],
        // serialization: [ { required: true, message: '请填机身串码', trigger: ['blur', 'change']} ],
        // mobile: [ { required: true, message: '请填手机号', trigger: ['blur', 'change']} ],
        // macAddress: [ { required: true, message: '请填Mac地址', trigger: ['blur', 'change']} ],
        // bluetoothAddress: [ { required: true, message: '请填蓝牙通讯地址', trigger: ['blur', 'change']} ],
      },
      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      categoryData: {
        brandList: [],
        deviceList: [],
        deviceTypeList: [],
        personList: []
      },
      deviceList: []
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onchangeSTime(e) {
      this.pram.detectionTime = e
    },
    initEditData() {
      this.parentOptions = [...this.allTreeList]
      if (this.isCreate === 1) {
        this.selectAmmunitionById()
      }

    },
    selectAmmunitionById() {
      api.selectMilitarySportsByIdApi({id: this.id}).then(data => {
        if (data) {
          this.pram = data
        }

      })
    },
    handlerSubmit: Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      api.insertMilitarySportsApi(this.pram).then(data => {
        this.$message.success('创建成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      api.insertMilitarySportsApi(this.pram).then(data => {
        this.$message.success('更新成功')
        this.$emit('hideEditDialog')
      })
    }
  }
}
</script>

<style scoped>

</style>
